package top.lshaci.learning.springboot.web.fastjson.reader;

import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.reader.ObjectReader;

import java.lang.reflect.Type;
import java.time.Month;

/**
 * ObjectReaderMonth
 *
 * @author liangxu
 */
public class ObjectReaderMonth implements ObjectReader<Month> {

    public static final ObjectReaderMonth INSTANCE = new ObjectReaderMonth();

    @Override
    public Month readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        if (jsonReader.nextIfNull()) {
            return null;
        }
        String str = jsonReader.readString();
        try {
            return Month.valueOf(str.toUpperCase());
        } catch (Exception e) {

        }
        return Month.of(Integer.parseInt(str));
    }
}
